﻿Imports System
Imports System.IO

Public Class ConnectSetup
    Public Sub Find_LANG()
        cmbLang.Items.Clear()

        Try
            ' Only get files that begin with the letter "c."
            Dim dirs As String() = Directory.GetFiles(PrPath & "lang\", "*.ini")
            ' Console.WriteLine("The number of files starting with c is {0}.", dirs.Length)
            Dim dir As String

            For Each dir In dirs
                Dim d() As String
                d = Split(dir, "\")

                cmbLang.Items.Add(d(d.Length - 1))
            Next
        Catch e1 As Exception
            'Console.WriteLine("The process failed: {0}", e1.ToString())
        End Try


    End Sub

    Private Sub cmbLang_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbLang.SelectedIndexChanged

        Dim objIniFile As New IniFile(PrPath & "base.ini")
        objIniFile.WriteString("general", "LANG", cmbLang.Text)


        sLANGPATH = PrPath & "lang\" & cmbLang.Text

        Call LANG_frmLogin()

    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveCfgButton.Click

        Dim objIniFile As New IniFile(PrPath & "base.ini")

        objIniFile.WriteString("DB", "DB", Me.cmbSUBD.Text)

        If Me.cmbSUBD.Text <> "MS Access" Then
            objIniFile.WriteString("DB", "SERVER", Me.server_ip.Text)
            objIniFile.WriteString("DB", "PORT", Me.server_port.Text)
            objIniFile.WriteString("DB", "BD", Me.db_name.Text)
            objIniFile.WriteString("DB", "USER", Me.dbu_name.Text)
            objIniFile.WriteString("DB", "PASSWORD", Me.db_pass.Text)
            objIniFile.WriteString("general", "BasePath", BasePath)
        Else
            objIniFile.WriteString("general", "file", cmbBD.Text)
            objIniFile.WriteString("DB", "SERVER", "")
            objIniFile.WriteString("DB", "PORT", "")
            objIniFile.WriteString("DB", "BD", "")
            objIniFile.WriteString("DB", "USER", "")
            objIniFile.WriteString("DB", "PASSWORD", "")
        End If
        Me.Hide()
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        TestDatabaseConnection(server_ip.Text, server_port.Text, db_name.Text, dbu_name.Text, db_pass.Text)
    End Sub

    Private Sub Find_Access_DB()
        cmbBD.Items.Clear()

        Try
            ' Only get files that begin with the letter "c."
            Dim dirs As String() = Directory.GetFiles(BasePath, "*.mdb")
            ' Console.WriteLine("The number of files starting with c is {0}.", dirs.Length)
            Dim dir As String

            For Each dir In dirs
                Dim d() As String
                d = Split(dir, "\")

                cmbBD.Items.Add(d(d.Length - 1))
            Next
        Catch e1 As Exception
            'Console.WriteLine("The process failed: {0}", e1.ToString())
        End Try


    End Sub

    Public Sub User_fill()

        On Error GoTo err_

        If DATAB = False Then
            Call LoadDatabase()
            Exit Sub
        Else
        End If

        frmLogin.cmbUser.Items.Clear()

        Dim rs As ADODB.Recordset

        Dim zcn As Integer
        Dim sSQL As String

        sSQL = "select count(*) as t_n from T_User"
        rs = New ADODB.Recordset
        rs.Open(sSQL, DB7, ADODB.CursorTypeEnum.adOpenDynamic, ADODB.LockTypeEnum.adLockOptimistic)

        With rs
            zcn = .Fields("t_n").Value

        End With
        rs.Close()
        rs = Nothing

        If zcn = 0 Then
            sSQL = "select * from T_User"
            rs = New ADODB.Recordset
            rs.Open(sSQL, DB7, ADODB.CursorTypeEnum.adOpenDynamic, ADODB.LockTypeEnum.adLockOptimistic)

            With rs
                .AddNew()
                .Fields("User_ID").Value = "ADMINISTRATOR"
                .Fields("Password").Value = "SWY\X"
                .Fields("Name").Value = "ADMINISTRATOR"
                .Fields("Level").Value = "Admin"

                .Update()
            End With
            rs.Close()
            rs = Nothing

        End If


        rs = New ADODB.Recordset
        sSQL = "select * from T_User WHERE Name <> ' '  ORDER BY Name"

        rs.Open(sSQL, DB7, ADODB.CursorTypeEnum.adOpenDynamic, ADODB.LockTypeEnum.adLockOptimistic)

        With rs
            .MoveFirst()
            Do While Not .EOF

                frmLogin.cmbUser.Items.Add(.Fields("Name").Value)

                .MoveNext()
                'DoEvents
            Loop
        End With
        rs.Close()
        rs = Nothing


        rs = New ADODB.Recordset
        rs.Open("UPDATE spr_other SET C='0' WHERE C=''", DB7, ADODB.CursorTypeEnum.adOpenDynamic, ADODB.LockTypeEnum.adLockOptimistic)
        'rs.Close()
        rs = Nothing


        Exit Sub

err_:

        MsgBox(Err.Description)
    End Sub

    Private Sub btnDBDir_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDBDir.Click
        Dim DirectoryBrowser As New FolderBrowserDialog

        ' Then use the following code to create the Dialog window
        ' Change the .SelectedPath property to the default location
        With DirectoryBrowser
            ' Desktop is the root folder in the dialog.

            .RootFolder = Environment.SpecialFolder.Desktop

            If Len(BasePath) = 0 Then
                ' Select the C:\Windows directory on entry.
                .SelectedPath = PrPath
            Else
                .SelectedPath = BasePath
            End If

            ' Prompt the user with a custom message.
            .Description = "Select the source directory"

            If .ShowDialog = DialogResult.OK Then
                ' Display the selected folder if the user clicked on the OK button.
                BasePath = .SelectedPath
            End If

        End With

        Dim objIniFile As New IniFile(PrPath & "base.ini")
        objIniFile.WriteString("general", "BasePath", BasePath & "\")

        BasePath = BasePath & "\"
        Call Find_Access_DB()
    End Sub



    Private Sub cmbBD_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbBD.SelectedIndexChanged
        If Len(BasePath) = 0 Then
            BasePath = Directory.GetParent(Application.ExecutablePath).ToString & "\database\"
        End If

        If cmbBD.Text = "" Or Len(cmbBD.Text) = 0 Then Exit Sub

        Base_Name = cmbBD.Text

        If DATAB = True Then

            UnLoadDatabase()
            Call LoadDatabase()

        Else
            Call LoadDatabase()
        End If
        Call User_fill()
    End Sub

    Private Sub ConnectSetup_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim allDriversList() As ODBCDriver
        Dim driverStr As String
        Dim i As Integer
        Dim SetupIniFile As New IniFile(PrPath & "base.ini")
        Dim dbType As String


        'MS(Access)
        'MS(SQL)
        'MS SQL 2008
        'PostgreSQL()
        'MySQL (MyODBC 3.51)
        'MySQL (MyODBC 5.1)
        'DSN()

        allDriversList = ODBCManager.GetODBCDrivers()

        cmbSUBD.Items.Add("MS Access")
        cmbSUBD.Items.Add("DNS")

        For i = 0 To UBound(allDriversList)
            driverStr = allDriversList(i).GetODBCDriverName()
            If (InStr(LCase(driverStr), "mysql") > 0) Then
                cmbSUBD.Items.Add("MySQL")
            End If
            If (InStr(LCase(driverStr), "sql server") > 0) Then
                cmbSUBD.Items.Add("MS SQL")
            End If
            If (InStr(LCase(driverStr), "sql server native") > 0) Then
                cmbSUBD.Items.Add("MS SQL 2008")
            End If
            If (InStr(LCase(driverStr), "postgresql unicode") > 0) Then
                cmbSUBD.Items.Add("PostgreSQL")
            End If
            'cmbSUBD.Items.Add(driverStr)
        Next i
        dbType = SetupIniFile.GetString("DB", "DB", "")
        Me.cmbSUBD.Text = dbType
        Me.cmbBD.Text = SetupIniFile.GetString("general", "file", "")

    End Sub

    Private Sub cmbSUBD_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbSUBD.SelectedIndexChanged

        Select Case cmbSUBD.Text

            Case "MS Access"
                lblServer.Text = "Server:"
                server_ip.Enabled = False
                server_port.Enabled = False
                db_pass.Enabled = False
                dbu_name.Enabled = False
                db_name.Visible = False
                db_name.Enabled = True
                cmbBD.Visible = True
                btnDBDir.Enabled = True
            Case "DSN"
                lblServer.Text = "DSN:"
                server_ip.Enabled = True
                server_port.Enabled = False
                db_pass.Enabled = True
                dbu_name.Enabled = True
                db_name.Visible = True
                db_name.Enabled = False
                cmbBD.Visible = False
                btnDBDir.Enabled = False
            Case Else
                lblServer.Text = "Server:"
                server_ip.Enabled = True
                server_port.Enabled = True
                db_pass.Enabled = True
                dbu_name.Enabled = True
                db_name.Visible = True
                db_name.Enabled = True
                cmbBD.Visible = False
                btnDBDir.Enabled = False



        End Select


    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Me.Hide()
    End Sub
End Class